package com.glaway.car.Service.impl;

import com.glaway.car.Service.AdminService;
import com.glaway.car.entity.Admin;
import com.glaway.car.mapper.AdminMapper;
import com.glaway.car.properties.JwtProperties;
import com.glaway.car.utils.JwtUtils;
import com.glaway.car.vo.LoginVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;

@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    private AdminMapper adminMapper;
    @Autowired
    private JwtProperties jwtProperties;

    @Override
    public LoginVO login(Admin admin)
    {
        Admin userinfo = adminMapper.getUsernameAndPermission(admin);
        if(userinfo != null){
            //1. 生成JWT令牌
            Map<String,Object> dataMap = new HashMap<>();
            dataMap.put("id", userinfo.getId());
            dataMap.put("username", userinfo.getUsername());
            dataMap.put("permission", userinfo.getPermission());

            String jwt = JwtUtils.generateJwt(dataMap, jwtProperties.getSecretKey(), jwtProperties.getTtl());
            return new LoginVO(userinfo.getId(),  userinfo.getPermission(), userinfo.getUsername(), jwt);
        }
        return null;
    }
}
